<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>
    
    指针变量作为函数参数 - DSRBLOG
    
  </title>
  

  
  
  <link href="atom.xml" rel="alternate" title="DSRBLOG" type="application/atom+xml">
  <link rel="stylesheet" href="asset/css/style.min.css">
  <link rel="stylesheet" href="asset/css/doc.css">
  <script src="asset/app.js"></script>


</head>

<body> <section class="ct-body">
	<div class="container">
		<div class="columns is-variable bd-klmn-columns is-4 is-centered">
			<div class="column is-four-fifths">
				<div class="post-body single-content">
					
					<h1 class="title">
						指针变量作为函数参数
					</h1>


					<div class="media">
						
						
						<div class="media-content">
							<div class="content">
								<p>
									<span class="date">2022/01/19 21:24 下午</span>
									

									

									<br />
									<span class="tran-tags">Tags:</span>&nbsp;
									
									<a class="tag is-link is-light">#C语言</a>
									

								</p>
							</div>
						</div>

					</div>
				</div>
				<article class="markdown-body single-content">
					<p>函数的参数不仅可以是整型、浮点型、字符型等数据，还可以是指针类型。它的作用是将一个变量的地址传送到另一个函数中。</p>
<h3><a id="%E4%BE%8B" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>例</h3>
<p>对输入的两个整数按大小顺序输出</p>
<h4><a id="%E5%88%86%E6%9E%90" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>分析</h4>
<p>本题是将指向两个变量的指针变量作为实参传递给形参的指针变量，在形参中通过指针交换两个变量的值</p>
<pre class="line-numbers"><code class="language-c">#include &lt;stdio.h&gt;

void swap(int *, int *); //全局函数声明

int main()
{
    int x, y;
    scanf(&quot;%d %d&quot;, &amp;x, &amp;y);
    
    printf(&quot;%d %d\n&quot;, x, y);
    printf(&quot;%d %d\n&quot;, &amp;x, &amp;y);
    
    if (x &lt;= y)       //判断大小
        swap(&amp;x, &amp;y); //传入实参
        
    printf(&quot;%d %d\n&quot;, x, y);
    printf(&quot;%d %d\n&quot;, &amp;x, &amp;y);
    return 0;
}

void swap(int *pa, int *pb) //交换pa、pb所指向的变量的值
{
    int t;
    t = *pa;
    *pa = *pb;
    *pb = t;
}
</code></pre>

				</article>
				<div class="comments-wrap">
					<div class="share-comments">
						

						

						
					</div>
				</div><!-- end comments wrap -->
			</div>
		</div><!-- end columns -->
	</div><!-- end container -->
</section>  














<script src="asset/prism.js"></script>









</body>

</html>